package com.ruoyi.yipaiju.controller;

import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.ImmutableMap;
import com.ruoyi.common.constant.BizConstants;
import com.ruoyi.common.core.domain.ApiResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.CurrencyUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpRequest;
import com.ruoyi.common.utils.http.HttpRespons;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.yipaiju.domain.SmsModel;
import com.ruoyi.yipaiju.service.ICommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("")
public class CommonController {

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private ICommonService commonService;

    /**
     * 获取地址
     * @param latitude
     * @param longitude
     * @return
     */
    @GetMapping("/getAddress")
    public ApiResult getAddress(String latitude, String longitude)
    {
        return commonService.getAddress(latitude, longitude);
    }

    /**
     * 税费计算器
     * @param jsonObject
     * @return
     */
    @PostMapping("/calculator")
    public ApiResult calculator(@RequestBody JSONObject jsonObject){
//        {"address":"广州","house_type":"普通住宅","transaction_type":"法院拍卖","inside_area":"100","built_area":"123","transferor":"个人","transferee":"个人","is_only":"否","register_date":"2020-11-27","deed_tax":0,"invoice_price":"123456","price":"12345678","house_num":"首套"}
        System.out.println(jsonObject.toJSONString());
        // TODO 不知道咋算。。。  再说  先返回一点死数据
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("value_added_tax", "1");
        paramsMap.put("individual_income_tax", "2");
        paramsMap.put("land_value_added_tax", "3");
        paramsMap.put("stamp_duty1", "44");
        paramsMap.put("land_transfer_fee", "5");
        paramsMap.put("deed_tax", "6");
        paramsMap.put("stamp_duty2", "7");
        paramsMap.put("register_price", "8");
        paramsMap.put("deed_tax", "9");
        paramsMap.put("land_value_added_tax", "10");
        paramsMap.put("individual_income_tax", "11");
        paramsMap.put("total", "12");
        paramsMap.put("value_added_tax", "13");
        paramsMap.put("individual_income_tax", "14");
        paramsMap.put("land_value_added_tax", "15");

        return ApiResult.success(paramsMap);
    }

    /**
     * 发送短信验证码
     * @return
     */
    @PostMapping("/sendsms")
    public ApiResult sendsms(@RequestBody SmsModel smsModel){
        String timeout = sysConfigService.selectConfigByKey("biz.user.code.timeout");
        String user = sysConfigService.selectConfigByKey("biz.user.code.user");
        String password = sysConfigService.selectConfigByKey("biz.user.code.password");
        String content = sysConfigService.selectConfigByKey("biz.user.code.content."+smsModel.getTemplate());
        timeout = StringUtils.isEmpty(timeout) ? BizConstants.Sms.TIMEOUT : timeout;
        user = StringUtils.isEmpty(user) ? BizConstants.Sms.USER : user;
        password = StringUtils.isEmpty(password) ? BizConstants.Sms.PASSWORD : password;
        content = StringUtils.isEmpty(content) ? BizConstants.Sms.CONTENT : content;

        HttpRequest httpRequest = new HttpRequest();
        String uuid = IdUtils.randomUUID();
        String code = CurrencyUtil.create(6); // 放到redis里
        content = String.format(content, code);
        String phone = StringUtils.isEmpty(smsModel.getPhone()) ? SecurityUtils.getLoginUser().getXcxUser().getPhone() : smsModel.getPhone();
        if (StringUtils.isNotEmpty(phone)){
            redisCache.setCacheObject("code:"+uuid, code, Integer.parseInt(timeout), TimeUnit.SECONDS);
            String url = BizConstants.Sms.URL + "?u="+user+"&p="+password+"&m="+phone+"&c="+content;
            HttpRespons httpRespons = httpRequest.sendGet(url);
            if (httpRespons != null && "0".equals(httpRespons.getContent())){
                return ApiResult.success(ImmutableMap.of("uuid", uuid));
            }
        }
        return ApiResult.error();
    }


}
